#include "subarraySum.h"
int subarraySum(std::vector<int>& nums, int k) {
  const int n = nums.size();
  int count = 0;
  std::unordered_map<int, int> cum;
  cum[0] = 1;
  int pre_sum = 0;
  for (auto num : nums) {
    pre_sum += num;
    if (cum.find(pre_sum - k) != cum.end()) count += cum[pre_sum - k];
    cum[pre_sum]++;
  }
  return count;
}
